import Vue from 'vue'
import VueRouter from 'vue-router'
// @:src当前路径别名
//好处：他不受当前路径影响
import Login from '@/views/login/index.vue'
import Home from '@/views/home/index.vue'
import ClockIn from '@/views/clockIn/index.vue'
import ClassControl from '@/views/classControl/index.vue'
import ClassData from '@/views/classData/index.vue'
import ClassMessage from '@/views/classMessage/index.vue'
import LeaveApproved from '@/views/leaveApproved/index.vue'

import Layout from '@/views/layout/index.vue'


Vue.use(VueRouter)

const routes = [
    {
        path: '/login',
        name: 'login',
        component: Login
    },
    {
        path: '/',
        // 命名路由layout有一个默认子路由，这个名字没有意义
        // 正确做法 ：如果有默认子路由，就不给父路由起名字
        // name: 'layout',
        component: Layout,
        // children:数组形式存储
        children: [
            {
                path: '',
                name: 'home',
                component: Home
            },
            {
                path: '/clockIn',
                name: 'clockIn',
                component: ClockIn
            },
            {
                path: '/classControl',
                name: 'classControl',
                component: ClassControl
            },
            {
                path: '/classData',
                name: 'classData',
                component: ClassData
            },
            {
                path: '/classMessage',
                name: 'classMessage',
                component: ClassMessage
            },
            {
                path: '/leaveApproved',
                name: 'leaveApproved',
                component: LeaveApproved
            }
        ]
    },
]

const router = new VueRouter({
    routes
})

// 路由导航守卫：所有页面导航都会经过这里
// router.beforeEach((to, from, next) => {

//     const user = JSON.parse(window.localStorage.getItem('user'))
//     //next() 放行、
//     if (to.path !== '/login') {
//         if(user) {
//             next()
//         }else{
//             next('/login')
//         }
//     }else{
//         // 登录页面，正常允许通过
//         next()
//     }
// })
export default router